app.controller("blogController", function($scope, $controller, $location,
		blogTypeService, blogService) {

	$controller('baseController', {
		$scope : $scope
	});// 继承
	$scope.blogList = [];
	$scope.blogType = {};
	$scope.entity ={};
	$scope.findAllType = function() {
		$scope.blogId = $location.search()['blogId'];
		if ($scope.blogId != null) {
			$scope.findBolgById($scope.blogId);
		}else{
			$scope.entity.pic="/title_pics/default.jpg";
		}
		blogTypeService.findAll().success(function(response) {
			$scope.blogTypeList = response;
		});
	}
	function checkForm(){
		
		editor.sync();//更新富文本
		var html = $("#editor_id").val();
		$scope.entity.content = html;
		
		var entity = $scope.entity;
		if(entity.title==""||entity.title==null)return false;
		if(entity.introduction==""||entity.introduction==null)return false;
		if(entity.clicknum==""||entity.clicknum==null)return false;
		if(entity.typeid==""||entity.typeid==null)return false;
		if(entity.replynum==""||entity.replynum==null)return false;
		if(entity.content==""||entity.content==null)return false;
		return true;
	}
	// 保存，修改
	$scope.save = function() {
		if (checkForm()) {
			var methodName = "add";
			if ($scope.blogId != null) {
				methodName = "update";
			}
			blogService.save(methodName, $scope.entity).success(
					function(response) {
						if (response.success) {
							alert(response.message);
							window.location.href = "blog.html";
						} else {
							alert(response.message)
						}
					});

		} else {

		}
	}

	$scope.findBlogList = function(page, rows) {
		$scope.typeId = $location.search()['typeId'];
		if ($scope.typeId == null) {
			blogService.findBlogList(page, rows).success(function(response) {
				$scope.blogList = response.list;
				$scope.pageNum = response.pageNum;
				$scope.currentPage = response.currentPage;
				// 创建页码数组。用于页面angularjs遍历显示页码 ps:这个方法好像有点不妥，没办法时间有限
				var pageList = new Array($scope.pageNum);
				for (var i = 0; i < pageList.length; i++) {
					pageList[i] = i + 1;
				}
				$scope.pageList = pageList;
			});
		} else {
			// 存在typeId，根据分类查询博客列表，并且回显分类名称
			blogService.findBlogListByTypeId(page, rows, $scope.typeId)
					.success(function(response) {
						$scope.blogList = response.list;
						$scope.pageNum = response.pageNum;
						$scope.currentPage = response.currentPage;
						// 创建页码数组。用于页面angularjs遍历显示页码 ps:这个方法好像有点不妥，没办法时间有限
						var pageList = new Array($scope.pageNum);
						for (var i = 0; i < pageList.length; i++) {
							pageList[i] = i + 1;
						}
						$scope.pageList = pageList;
					});
			// 回显分类名称
			blogTypeService.findBlogTypeByTypeId($scope.typeId).success(
					function(response) {
						$scope.blogType = response;
					});
		}
	}

	// 显示blog详细内容
	$scope.blog = {};
	$scope.showContent = function() {
		$scope.blogId = $location.search()['blogId'];
		blogService.findBlogById($scope.blogId).success(function(response) {
			$scope.blog = response;
			$(function() {
				$("#content").html($scope.blog.content);
			});
		});
	}

	// 查询博客列表(带搜索条件
	$scope.searchEntity = {};// 初始化查询条件
	$scope.search = function(page, rows) {
		blogService.search(page, rows, $scope.searchEntity).success(
				function(response) {
					$scope.blogList = response.list;
					$scope.total = response.total;
				});
	}

	// 执行分页插件先查询博客列表获取总数
	$scope.pageInit = function(page, rows) {
		blogService.search(page, rows, $scope.searchEntity).success(
				function(response) {
					$scope.blogList = response.list;
					$scope.total = response.total;
					$scope.page($scope.total);
				});
	}

	// 将总数传入分页插件
	$scope.page = function(total) {
		layui.use('laypage', function() {
			var laypage = layui.laypage;
			// 执行一个laypage实例
			laypage.render({
				elem : 'page' // 注意，这里的 test1 是 ID，不用加 # 号
				,
				count : total// 数据总数，从服务端得到
				,
				limit : 4,
				jump : function(obj, first) {
					$scope.search(obj.curr, obj.limit);
				}
			});
		});
	}

	// 删除
	$scope.del = function() {
		if (confirm("确定要删除吗？")) {
			blogService.del($scope.selectIds).success(function(response) {
				if (response.success) {
					alert(response.message);
					window.location.reload();
				} else {
					alert(response.message);
				}
			});
		}

	}

	// 根据id查询
	$scope.findBolgById = function(blogId) {
		blogService.findBlogById(blogId).success(function(response) {
			$scope.entity = response;
			editor.html($scope.entity.content);
		});
	}
	$(function(){
		$("#upfile").on("change",function(){
			 var image = document.getElementById("upfile").files[0];
			 var formData = new FormData();
	         formData.append('image', image);
			 $.ajax({
				 url:"blog/upImgFile.do",
				 type:"post",
				 data:formData,
	             contentType:false,
	             processData:false,
				 success:function(data){
					 
					 $("#img").attr("src",data);
					 $scope.entity.pic=data;
					 alert( $scope.entity.pic); 
				 }
			 });
		});
	});

});